﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.Settings;
using HtmlAgilityPack;

namespace Settings.Forms
{
    /// <summary>
    /// Support for existing "predefined" files
    /// </summary>
    public class DifferentFileAdapter
    {
        public static PageSettings ConvertToPageSetting(string fileName)
        {
            var pageSettings = new PageSettings();
            var htmlDoc = new HtmlDocument();
            htmlDoc.Load(fileName);
            pageSettings.Fields = ConvertToFields(htmlDoc.DocumentNode,
                                         node => new FieldSettings()
                                         {
                                             ElementID = node.GetAttributeValue("id", string.Empty).Replace("$", "_"),
                                             ElementName = node.GetAttributeValue("name", string.Empty)
                                         })
                                  .ToArray();
            return pageSettings;
        }
        private static IEnumerable<FieldSettings> ConvertToFields(HtmlNode htmlNode, Func<HtmlNode, FieldSettings> baseCreator)
        {
            foreach (var inputText in htmlNode.SelectNodes("//input[@type='TEXT']"))
            {
                var baseField = baseCreator(inputText);
                baseField.InputType = "text";
                baseField.InputValue = inputText.GetAttributeValue("value", string.Empty);
                yield return baseField;
            }

            foreach (var inputRadio in htmlNode.SelectNodes("//input[@type='radio']"))
            {
                var baseField = baseCreator(inputRadio);
                baseField.InputType = "radio";
                baseField.InputValue = inputRadio.GetAttributeValue("value", string.Empty);
                baseField.Checked = true;
                yield return CorrecntRadioInput(baseField);
            }

            foreach (var inputCheckbox in htmlNode.SelectNodes("//input[@type='checkbox']"))
            {
                var baseField = baseCreator(inputCheckbox);
                baseField.InputType = "checkbox";
                baseField.Checked = inputCheckbox.GetAttributeValue("checked", string.Empty) == "checked";
                yield return baseField;
            }

            foreach (var select in htmlNode.SelectNodes("//select"))
            {
                var baseField = baseCreator(select);
                baseField.InputType = "select";
                baseField.InputValue = select.FirstChild.GetAttributeValue("value", string.Empty);
                yield return baseField;
            }
        }

        private static FieldSettings CorrecntRadioInput(FieldSettings baseField)
        {
            if (RadioInputsCorrections.ContainsKey(baseField.ElementName))
                baseField.ElementID = RadioInputsCorrections[baseField.ElementName][baseField.InputValue];
            return baseField;
        }

        private static readonly Dictionary<string, Dictionary<string, string>> RadioInputsCorrections = new Dictionary<string, Dictionary<string, string>>
                           {
                               {
                                   "daneOs$rbPlec",
                                   new Dictionary<string, string>
                                       {
                                           {"M", "daneOs_rbPlec_0"},
                                           {"F", "daneOs_rbPlec_1"}
                                       }
                                   },
                               {
                                   "daneOs$rbStanCywilny",
                                   new Dictionary<string, string>
                                       {
                                           {"KP", "daneOs_rbStanCywilny_0"},
                                           {"ZZ", "daneOs_rbStanCywilny_1"},
                                           {"SP", "daneOs_rbStanCywilny_2"},
                                           {"RR", "daneOs_rbStanCywilny_3"},
                                           {"WW", "daneOs_rbStanCywilny_4"},
                                           {"IN", "daneOs_rbStanCywilny_5"}
                                       }
                                   }
                           };

    }
}